<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="账号" prop="account">
        <el-input v-model.trim="form.account" placeholder="请输入账号" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model.trim="form.phone" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model.trim="form.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model.trim="form.nickName" placeholder="请输入昵称" />
      </el-form-item>
      <el-form-item label="启用状态" prop="isEnable">
        <el-radio-group v-model="form.isEnable">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">停用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button :loading="isSaving" type="primary" @click="save">
        确 定
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/userManagement'

  export default {
    name: 'UserManagementEdit',
    data() {
      const validateEnable = (rule, value, callback) => {
        if (value !== 0 && value !== 1) {
          callback(new Error('请选择状态'))
        } else {
          callback()
        }
      }
      return {
        form: {
          account: '',
          phone: '',
          email: '',
          nickName: '',
          isEnable: 1,
        },
        rules: {
          account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
          email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
          phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
          nickName: [
            { required: true, message: '请输入昵称', trigger: 'blur' },
          ],
          isEnable: [{ validator: validateEnable, trigger: 'blur' }],
        },
        title: '',
        dialogFormVisible: false,
        isSaving: false,
      }
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加用户'
        } else {
          this.title = '编辑用户'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            try {
              this.isSaving = true
              const res = await doEdit(this.form)
              let successMsg = '添加成功'
              let errorMsg = '添加失败'
              if (this.form.id) {
                successMsg = '修改成功'
                errorMsg = '修改成功'
              }
              if (res && res.status === 0) {
                this.$baseMessage(
                  res.msg || successMsg,
                  'success',
                  'vab-hey-message-success'
                )
                this.$emit('fetch-data')
                this.close()
              } else {
                this.$baseMessage(
                  res ? res.msg : errorMsg,
                  'error',
                  'vab-hey-message-error'
                )
              }
            } catch (e) {
              console.log(e)
              this.$baseMessage(
                e ? e.message : '操作失败',
                'error',
                'vab-hey-message-error'
              )
            }
            this.isSaving = false
          }
        })
      },
    },
  }
</script>
